﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using CalcSharp.Core.IO;

namespace CalcSharp.Core
{
    public sealed class CoreInit
    {
        static bool FrameWork35;
        static bool SQLServerCE35;
        static bool UnixorMono;
        static bool RwError;

        public static bool IntializeCore()
        {
            UnixorMono = false;
            SQLServerCE35 = false;
            FrameWork35 = false;
            bool succes = true;
            RwError = false;

            int p = (int)Environment.OSVersion.Platform;
            string pf = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);

            if ((p == 4) || (p == 6) || (p == 128) || Type.GetType("Mono.Runtime") != null)
            {
                UnixorMono = true;
                succes = false;
            }
            if (!Directory.Exists(pf + @"\Reference Assemblies\Microsoft\Framework\v3.5"))
            {
                FrameWork35 = true;
                succes = false;
            }
            if (!Directory.Exists(pf + @"\Microsoft SQL Server Compact Edition\v3.5"))
            {
                SQLServerCE35 = true;
                succes = false;
            }

            try
            {
                if (!Directory.Exists(FileHelper.ProfileDir+@"\CalcSharpUserData"))
                {
                    Directory.CreateDirectory(FileHelper.ProfileDir+@"\CalcSharpUserData");
                }
            }
            catch (IOException) { RwError = true; succes = false; }

            return succes;
        }

        public static void ErrorMesage()
        {
            StringBuilder errors = new StringBuilder();
            if (UnixorMono)
            {
                errors.Append("This Application can't run on Unix hosts or you are runing it with Mono");
                MessageBox.Show("This application can't run because of the following problem(s):\r\n" + errors.ToString(), "Application error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (FrameWork35 || SQLServerCE35)
            {
                if (FrameWork35) errors.Append(".NET Framework version 3.5\r\n"); 
                    if (SQLServerCE35) errors.Append("SQL Server Compact edition 3.5\r\n");
                MessageBox.Show("This application can't run because the following components are not installed:\n\r" + errors.ToString(), "Application error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (RwError)
            {
                errors.Append("Application can't start, because " + FileHelper.ProfileDir+@"\CalcSharpUserData Directory Doesn't exist and can't be created");
                MessageBox.Show("This application can't run because of the following problem(s):\r\n" + errors.ToString(), "Application error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
    }
}
